import Vue from "vue";
import Router from "vue-router";
import routerConfig from "./router-config";
import store from '../store'
import * as types from '../store/mutation-types'
Vue.use(Router);

const router = new Router({
  // mode: 'history',
  scrollBehavior: () => ({
    y: 0
  }),
  routes: routerConfig
});

let pageBackFlag = true;

let push = router.push;
router.push = function(){
    pageBackFlag = false;
    push.apply(this,arguments);
};

let replace = router.replace;
router.replace =  function(){
    pageBackFlag = false;
    replace.apply(this,arguments);
};

let go = router.go;
router.go =  function(){
    pageBackFlag = true;
    go.apply(this,arguments);
};


router.beforeEach((to, from, next) => {
  if(pageBackFlag){
    store.commit(types.UPDATE_DIRECTION, 'reverse')
  }else{
    store.commit(types.UPDATE_DIRECTION, 'forward')
  };
  next();
});

router.afterEach(() => {
  pageBackFlag = true;
});
export default router;
export const asyncRouterMap = [];
